package cn.kk.day02zuoye.controller;
/*
 *@author KK on 2022.11.22 20:03
 */

import cn.kk.day02zuoye.dao.UserDao;
import cn.kk.day02zuoye.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String age = request.getParameter("age");
        User user = new User(username, Integer.valueOf(age));
        UserDao userDao = new UserDao();
        List<User> users = userDao.getUsers();
        boolean add = users.add(user);
        if (add) {
            request.setAttribute("msg","注册成功");
            request.setAttribute("users", users);
        }
        request.getRequestDispatcher("index.jsp").forward(request,response);
    }
}